package com.supertange.miniapp.wepet.common.controller;

import com.supertange.miniapp.wepet.common.utils.DateUtil;
import com.supertange.miniapp.wepet.common.utils.FileUploadUtil;
import com.supertange.miniapp.wepet.common.utils.MyException;
import com.supertange.miniapp.wepet.common.vo.AjaxResult;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * 文件上传控制器
 *
 * @date 2017年10月17日 下午3:16:04
 */
@RestController
@RequestMapping("/upload")
public class FileUploadController extends BaseController {

    @PostMapping("/fileUpload")
    public AjaxResult upload(@RequestParam MultipartFile file) throws Exception {
        // 保存上传的文件

        String headImg = FileUploadUtil.upload(file, uploadRootPath, "/video");
        return AjaxResult.ok(headImg);
    }

    @PostMapping("/qnFileUpload")
    public AjaxResult qnUpload(@RequestParam MultipartFile file) throws Exception {
        // 保存上传的文件

        String headImg = FileUploadUtil.qnUpload(file,ACCESSKEY,SECRETKEY,BUCKET,HOST,"videoCover");
        return AjaxResult.ok(headImg);
    }

    /**
     * 通过bese64上传图片
     *
     * @param imgData
     * @param uploadPath
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadImgByBase64")
    public AjaxResult uploadImgByBase64(@RequestParam String imgData, String uploadPath) throws Exception {
        String dataPrix = "";
        String data = "";
        if (imgData == null || "".equals(imgData)) {
            throw new MyException("上传失败，上传图片数据为空");
        } else {
            String[] d = imgData.split("base64,");
            if (d != null && d.length == 2) {
                dataPrix = d[0];
                data = d[1];
            } else {
                throw new MyException("上传失败，数据不合法");
            }
        }
        String suffix = "";
        if ("data:image/jpeg;".equalsIgnoreCase(dataPrix)) {
            suffix = ".jpg";
        } else if ("data:image/gif;".equalsIgnoreCase(dataPrix)) {
            suffix = ".gif";
        } else if ("data:image/png;".equalsIgnoreCase(dataPrix)) {
            suffix = ".png";
        } else {
            throw new Exception("上传图片格式不合法");
        }
        uploadPath = "/" + uploadPath + "/" + DateUtil.getCurrentDate("yyyyMMdd") + "/";
        // 存储图片的物理路径
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        File file = new File(uploadRootPath + uploadPath + fileName);
        if (!new File(uploadRootPath + uploadPath).exists()) {
            new File(uploadRootPath + uploadPath).mkdirs();
        }
        // 进行解密
        byte[] result = Base64.decodeBase64(data);
        FileCopyUtils.copy(result, file);

        return AjaxResult.ok(uploadPath + fileName);
    }
}
